class DishesController < ApplicationController
  # GET /dishes
  # GET /dishes.json
  def index
    @dishes = Dish.all

    respond_to do |format|
      format.html # index.html.erb
      format.json  { render :json => @dishes }
    end
  end

  # GET /dishes/1
  # GET /dishes/1.json
  def show
    @dish = Dish.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json  { render :json => @dish }
    end
  end

  # GET /dishes/new
  # GET /dishes/new.json
  def new
    @dish = Dish.new

    respond_to do |format|
      format.html # new.html.erb
      format.json  { render :json => @dish }
    end
  end

  # GET /dishes/1/edit
  def edit
    @dish = Dish.find(params[:id])
  end

  # POST /dishes
  # POST /dishes.json
  def create
    @dish = Dish.new(params[:dish])

    respond_to do |format|
      if @dish.save
        format.html { redirect_to(@dish, :notice => 'Dish was successfully created.') }
        format.json  { render :json => @dish, :status => :created, :location => @dish }
      else
        format.html { render :action => "new" }
        format.json  { render :json => @dish.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /dishes/1
  # PUT /dishes/1.json
  def update
    @dish = Dish.find(params[:id])

    respond_to do |format|
      if @dish.update_attributes(params[:dish])
        format.html { redirect_to(@dish, :notice => 'Dish was successfully updated.') }
        format.json  { head :ok }
      else
        format.html { render :action => "edit" }
        format.json  { render :json => @dish.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /dishes/1
  # DELETE /dishes/1.json
  def destroy
    @dish = Dish.find(params[:id])
    @dish.destroy

    respond_to do |format|
      format.html { redirect_to(dishes_url) }
      format.json  { head :ok }
    end
  end

  def getDishesByConsume

    @dishes = Dish.where(:restaurant_id => params[:rest_id])

    @dishes.each do |d|

      d.rate = Dscore.where(:dish_id => d.id).length
        
    end

    @dishes.sort_by(&:rate) 

    respond_to do |format|
      format.json  { render :json => @dishes }
    end

  end


  def createDish

    @dish = Dish.new

    @dish.name = params[:name]
    @dish.descr = params[:descr]
    @dish.cost = params[:cost].gsub!(',','.')
    @dish.menu_id = 1
    @dish.status = 1
    @dish.restaurant_id = params[:restaurant_id]
    @dish.rate = 3

    @dish.save

     respond_to do |format|
      format.json  { render :json => @dish}
    end

  end

end
